package com.tm.szb.commons;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

        @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
        public R sqlException(SQLIntegrityConstraintViolationException exception){
            exception.printStackTrace();
            String message = exception.getMessage();
            String[] split = message.split("");
            return R.error(split[2]+"已存在");
        }


        @ExceptionHandler(BizException.class)
        public R sqlException(BizException exception){
            exception.printStackTrace();
            return R.error(exception.getCode(),exception.getDesc());
        }
}
